<?php
namespace data\set\news;
use \data\BaseData;
/**
 * 新闻管理
 */
class NewsData extends BaseData{
    
    /**
     * 读取一个管理员信息
     */
    public function find($param){
        $check = ['id','plat','tag'];
        $where = $this->parseWhere($check,$param);
        if( !$where )  dataE('查询条件不能为空');
        $info  = M('set_news')->where($where)->find();
        return $info;
    }
    
    /**
     * 添加一个新闻
     */
    public function add($data){               
        $check = [            
            'siteid'    => ['reg'=>'int'],
            'adduserid' => ['reg'=>'int'],            
            'groupid'   => ['reg'=>'int'],
            'tags'      => '',
            'status'    => ['val'=>1],           
            'datastatus'=> ['def'=>1,'reg'=>[1,2,3,4]], 
            'sort'      => ['def'=> 50,'reg'=>'int'],
            'addtime'   => ['val'=>SYS_TIME],
            'edittime'  => ['val'=>0],
            'pic'       => ['check'=>'must','msg'=> '请上传广告图片'],
            'name'      => '',
            'author'    => '',
            'des'       => '',
            'detail'    => '',
            'seo'       => ''            
        ];
        $save = $this->verifyData($check,$data);
        $id = M('set_news')->insertGetId($save);
        if( !$id )  dataE('添加新闻失败');
        return $id;
    }
    
    /**
     * 编辑
     */
    public function edit($id,$data){ 
        if( !$id ) dataE('修改参数不能为空');        
        $check = [
            'adduserid' => ['reg'=>'int'],            
            'groupid'   => ['reg'=>'int'],
            'tags'      => '',
            'status'    => ['reg'=>'int'],           
            'datastatus'=> ['reg'=>[1,2,3,4]], 
            'sort'      => ['reg'=>'int'],
            'edittime'  => ['val'=>SYS_TIME],
            'pic'       => '',
            'name'      => '',
            'author'    => '',
            'des'       => '',
            'detail'    => '',
            'seo'       => ''            
        ];      
        $save = $this->verifyData($check,$data);
        if( !$save ) return false;
        $where = ['id'=>$id];
        $result = M('set_news')->where($where)->update($save);
        if($result === false ) dataE('修改新闻信息失败');
        return $result;
    }
    
}